package io.jee.medusa.bittorrent;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;

public class BittorrentUtils {
	
	public static TorrentFile getTorrentFile(Map<?, ?> map){
		TorrentProcessor tp = new TorrentProcessor();
        TorrentFile t = null;
		try {
			t = tp.getTorrentFile(map);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
        return t;
	}
	
	public static Map<?, ?> readerTorrent(InputStream is) throws IOException{
		Map<?, ?> map = BDecoder.decode(new BufferedInputStream(is));
		return map;
	}
	
	public static Map<?, ?> readerTorrent(String path, String name) throws IOException{
		Map<?, ?> map = BDecoder.decode(new BufferedInputStream(new FileInputStream(new File(path, name))));
		return map;
	}
	
	public static void writeTorrent(Map<?, ?> map, String path, String name) throws IOException{
		FileOutputStream fileOutputStream = new FileOutputStream(new File(path, name));
		fileOutputStream.write(BEncoder.encode(map));
		fileOutputStream.flush();
		fileOutputStream.close();
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Map<String, ?> clearTorrent(Map<String, Map> map){
		Map info = (Map) map.get("info");
		
		if (info.containsKey("files")) {
            List multFiles = (List) info.get("files");
            for (int i = 0; i < multFiles.size(); i++) {
            	
            	Map files = (Map)multFiles.get(i);

                List path = (List) files.get("path");
                for (int j = 0; j < path.size(); j++) {
                	byte[] pathbyte = (byte[])path.get(j);
                	String oldName = new String(pathbyte);
                	int index = 0;
                	String newName;
                	if((index = oldName.lastIndexOf("."))>0){
                		String prefix = oldName.substring(0, index);
                		String Suffix = oldName.substring(index);
                		newName = DigestUtils.md5Hex(prefix)+Suffix;
                	}else{
                		newName = DigestUtils.md5Hex(oldName);
                	}
                	path.set(j, newName.getBytes());
                }
                files.put("path", path);
                multFiles.set(i, files);
            }
            info.put("files", multFiles);
        }
		
		String oldName = new String((byte[]) info.get("name"));
    	int index = 0;
    	String newName;
    	if((index = oldName.lastIndexOf("."))>0){
    		String prefix = oldName.substring(0, index);
    		String Suffix = oldName.substring(index);
    		newName = DigestUtils.md5Hex(prefix)+Suffix;
    	}else{
    		newName = DigestUtils.md5Hex(oldName);
    	}
		info.put("name", newName.getBytes());
		
		map.put("info", info);
		
		return map;
	}
	
	public static void main(String[] args) throws IOException {
//		Map map = readerTorrent("d:/abc", "cplnpkb.torrent");
//		TorrentFile torrentFile = getTorrentFile(map);
//		torrentFile.printData(true);
		
	}

}
